package faceb.server.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

import faceb.server.dao.CourseDao;
import faceb.server.dao.UserDao;
import faceb.server.model.AjaxResponse;
import faceb.server.model.Course;
import faceb.server.model.User;
import faceb.server.util.CharSetUtil;

@Controller
public class CourseController {

	@Resource(name = "courseDao")
	private CourseDao courseDao;
	
	@Resource(name = "userDao")
	private UserDao userDao;

	private Gson gson = new Gson();
	
	@RequestMapping(value = "/users/{userid}/courses")
	@ResponseBody
	// 将jsonStr直接返回而不是去模板库找模板进行渲染,由于默认输出会按照iso-8859-1进行编码，但是字符串本来是utf-8的，所以要让字符串转换为iso-8859-1的
	public String getCourseByUserId(@PathVariable("userid") String userid,HttpServletResponse resp){

		AjaxResponse ar = new AjaxResponse();
		Map<String, String>  courses= courseDao.getCoursesByUserId(userid);
		
		ar.setStatus("0");
		ar.setInfo("success");
		ar.setData(courses);
		
		return CharSetUtil.getRespBodyStr(gson.toJson(ar));
	}
	
	@RequestMapping(value = "/course/insert")
	@ResponseBody
	public String insertCourse(HttpServletRequest req,HttpServletResponse resp ) throws Exception{

		String c_id = req.getParameter("c_id");
		String term = req.getParameter("c_term_count");
		String content = req.getParameter("content");
		User user = (User) req.getSession().getAttribute("user");
		String u_id = user.getU_id();
		
		int c_term_count = Integer.valueOf(term);
		Course c = new Course(c_id, c_term_count, u_id, content);
		AjaxResponse ar = new AjaxResponse();
		int result = courseDao.insertCourse(c);
		
		if(result==1){
		ar.setStatus("0");
		ar.setInfo("success");
		ar.setData("");
		}
		
		return CharSetUtil.getRespBodyStr(gson.toJson(ar));
	}
	
	@RequestMapping(value = "/course/update")
	@ResponseBody
	public String updateCourse(HttpServletRequest req,HttpServletResponse resp ) throws Exception{

		String c_id = req.getParameter("c_id");
		String term = req.getParameter("c_term_count");
		String content = req.getParameter("content");
		User user = (User) req.getSession().getAttribute("user");
		String u_id = user.getU_id();
		
		int c_term_count = Integer.valueOf(term);
		Course c = new Course(c_id, c_term_count, u_id, content);
		AjaxResponse ar = new AjaxResponse();
		int result = courseDao.updateCourse(c);
		
		if(result==1){
		ar.setStatus("0");
		ar.setInfo("success");
		ar.setData("");
		}
		
		return CharSetUtil.getRespBodyStr(gson.toJson(ar));
	}
	
	
	
	@RequestMapping("/tpl/course/users/{userid}")
	public ModelAndView course(@PathVariable("userid")String userid){
		Map<String,String> params = new HashMap<String, String>();
		User u = userDao.getUserById(userid);
		if(u==null)
			return null ;
		params.put("userid", userid);
		params.put("username", u.getU_name());
		ModelAndView mv = new ModelAndView("course", params);
		return mv;
	}
}
